// pages/forgetPassword/forgetPassword.js
var util = require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my");
var encryption=require("../../utils/md5");


Page({
  /*页面的初始数据*/
  data: {
    mobile:'',
    mob1: '',
    focus: true,
    time: 60,
    length:6,
    isFocus:false,
    Value:"",
    show_btn: false,
    show_ver: false,
    show_else: false,
    password: '',
    password_ag: '',
    show_ti: false,
    text: "新密码不能为空"
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    
  },
  change_agree:function(){
    this.setData({agree: !this.data.agree})
  },
  // 手机号
  // mobile_focus(e){
  //   var that = this;
  //   var val = e.detail.value;
  //   var mob1 = util.insertStr(util.insertStr(val,3," "),8," ");
  //   that.setData({mobile:val, mob1:mob1})
  // }, 
  mobile_tap(e){
    var that = this;
    var val = e.detail.value;
    that.setData({mobile:val})
  },
  send_mobile:function(){
    var that = this,mobile = that.data.mobile,agree=that.data.agree;
    var neg=(/^1[3456789]\d{9}$/);
    if(!neg.test(mobile)||!mobile){
      util.page_show_toast('手机号码输入有误',1500)
      return false;
    }
    that.send_code(mobile)
  },
  // 验证码
  ver_focus(e){
    var that = this;
    var val = e.detail.value;
    that.setData({ Value:val })
    if(val.length == 6){
      //
      that.very_code(that.data.mobile,val)
    }
  }, 
  ver_tap(){
    var that = this;
    that.setData({ isFocus:true, })
  },
  // 倒计时
  interval:function(){
    var that = this,time=that.data.time;
    var tms=setInterval(function(){
      time--;
      that.setData({time: time})
      if(time==0){
        clearInterval(tms)
        that.setData({time: 60,show_btn: true})
      }
    },1000)
  },
  // 重新发送
  again_send:function(){
    var that = this,show_btn=that.data.show_btn;
    if(show_btn){
      //
      that.interval();
      that.setData({show_btn: false})
    }
  },
  // 密码输入
  input_password:function(e){
    this.setData({password: e.detail.value,show_ti:false})
  },
  input_again_password:function(e){
    this.setData({password_ag: e.detail.value,show_ti:false})
  },
  // 修改
  rev_password:function(){
    var that=this,password=that.data.password,password_ag=that.data.password_ag;
    if(!password||!password_ag){
      that.setData({show_ti:true})
      return false;
    }
    if(password.length<6||password_ag.length<6){
      that.setData({show_ti:true,text:"密码长度不能少于6为位"})
      return false;
    }
    if(password != password_ag){
      that.setData({show_ti:true,text:"密码和新密码输入不一致"})
      return false;
    }
    //
    that.set_password_code(that.data.mobile,password)
  },
  // 发送验证码请求
  send_code:function(mobile){
    var that=this;
    config.send_code({"phone":mobile}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        that.setData({show_ver: true,isFocus: true});
        that.interval();
        setTimeout(function(){
          util.page_show_toast('验证码已发送',1500)
        },500)
      }
    })
  },
  // 手机验证码验证请求
  very_code:function(mobile,code){
    var that=this;
    config.very_code({phone:mobile,code:code}).then(res=>{
      // console.log(res)
      if(res.data.code==200){
        that.setData({show_else: true})
      }else{
        util.page_show_toast(res.data.message,2000)
      }
    })
  },
  // 设置密码
  set_password_code:function(mobile,password){
    var that=this;
    var params={
      "phone": mobile,
      "password": encryption.md5(password)
    }
    my.set_password_code(params).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('修改成功',1500)
        setTimeout(function(){
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }else{
        util.page_show_toast(res.data.message,1500)
      }
    })
  },
})